home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / utility / utilcli / getinfo.lha / GetFileInfo.c < prev    next >
C/C++ Source or Header  |  1995-09-28  |  3KB  |  97 lines

  1. /* Program: GetFileInfo <device> [TYPE|NAME|PRO|SIZE|BLOCKS|COMMENT]
  2.    Desc: Program that gets info about a file and returns the
  3.    result in a global env. variable: File.
  4.    Author: P Hutchison 3/12/93
  5.    Modifications:
  6.    1. Use proto headers (25/6/95)
  7.    2. Use PutStr() and improve prototyping (28/9/95)
  8. */
  9.  
  10. #include <proto/exec.h>
  11. #include <proto/dos.h>
  12. #include <exec/types.h>
  13. #include <dos/dos.h>
  14. #include <dos/var.h>
  15. #include <string.h>
  16.  
  17. struct FileInfoBlock fileinfo =
  18. {0, 0, (char)NULL, 0, 0, 0, 0, NULL, (char)NULL, (char)NULL };
  19.  
  20. char buf[108];
  21.  
  22. void FetchInfo(char *opt);
  23.  
  24. main(int argc, char *argv[])
  25. {
  26.    struct DOSBase *DOSBase;
  27.    long __oslibversion = 37;
  28.    char name[5];
  29.    BPTR lock;
  30.    
  31.    strcpy(name, "File");
  32.    buf[0] = (char)NULL;
  33.    
  34.    if (DOSBase = (struct DOSBase *)OpenLibrary("dos.library", __oslibversion))
  35.    {      
  36.     if (argc == 3) /* NB: Command name counts as 1 also! */
  37.     {
  38.       if (lock = Lock(argv[1], ACCESS_READ))
  39.       {
  40.          if (Examine(lock, &fileinfo))
  41.          {
  42.           FetchInfo(argv[2]);
  43.           
  44.           SetVar(name, buf, -1, GVF_GLOBAL_ONLY); /* Set env. variable */
  45.          } 
  46.          UnLock(lock); 
  47.       }
  48.     } else
  49.       PutStr("GetFileInfo <file> [TYPE|NAME|PRO|SIZE|BLOCKS|COMMENT]\n");
  50.     CloseLibrary((struct Library *)DOSBase);
  51.    } 
  52. }
  53.       
  54. void FetchInfo(char *opt)
  55. {
  56.    long  result;
  57.    char  *prot_ptr;
  58.  
  59.    if (stricmp(opt, "TYPE") == 0)
  60.    {
  61.       result = fileinfo.fib_DirEntryType;
  62.       if (result < 0) strcpy(buf, "FILE");
  63.       if (result > 0) strcpy(buf, "DIRECTORY");
  64.    }
  65.    if (stricmp(opt, "NAME") == 0)
  66.       strcpy(buf, fileinfo.fib_FileName);
  67.             
  68.    if (stricmp(opt, "PRO") == 0)     /* Get Information */
  69.    {  
  70.       prot_ptr = &buf[0];
  71.       result = fileinfo.fib_Protection;
  72.       if (result & FIBF_SCRIPT) *prot_ptr++ = 'S';    
  73.       if (result & FIBF_PURE) *prot_ptr++ = 'P';    
  74.       if (result & FIBF_ARCHIVE) *prot_ptr++ = 'A';    
  75.       if (result & FIBF_READ) *prot_ptr++ = 'R';    
  76.       if (result & FIBF_WRITE) *prot_ptr++ = 'W';    
  77.       if (result & FIBF_EXECUTE) *prot_ptr++ = 'E';    
  78.       if (result & FIBF_DELETE) *prot_ptr++ = 'D';    
  79.       *prot_ptr = (char)NULL;
  80.    }
  81.    if (stricmp(opt, "SIZE") == 0) 
  82.    {
  83.       result = fileinfo.fib_Size;
  84.       stcl_d(buf, result);
  85.    }
  86.    if (stricmp(opt, "BLOCKS") == 0)
  87.    {
  88.       result = fileinfo.fib_NumBlocks;
  89.       stcl_d(buf, result);
  90.    }
  91.    if (stricmp(opt, "COMMENT") == 0)
  92.    {
  93.       strcpy(buf, fileinfo.fib_Comment);
  94.     }
  95. }
  96.         
  97.